#pragma once


#include "./collector.h"

#include "./fore_rtdb.h"

//#include "../rtdbms/scadaCnt.h" 

#ifdef WIN32
	#include "../net/simple_net_base.h" 
	#include"afxmt.h"
#else
	#include <pthread.h>
	 
	#include "./ZNet.h" 

	#include "./CSerial.h"
#endif


namespace MODBUS_APP
{  
	//������
	const unsigned char		FC_READ_COIL_STATUS				= 0x01;		//01 read coil status
	const unsigned char		FC_READ_INPUT_STATUS			= 0x02;		//02 read input status
	const unsigned char		FC_READ_HOLDING_REG				= 0x03;		//03 read holding register
	const unsigned char		FC_READ_INPUT_REG				= 0x04;		//04 read input register
	const unsigned char		FC_WRITE_SINGLE_COIL			= 0x05;		//05 write single coil
	const unsigned char		FC_WRITE_SINGLE_REG				= 0x06;		//06 write single register
	const unsigned char		FC_WRITE_MULTIPLE_COIL			= 0x0f;		//15 write multiple coil
	const unsigned char		FC_WRITE_MULTIPLE_REG			= 0x10;		//16 write multiple register
	
	//MODBUS <= 0x10
	const unsigned char		FC_GPIO_INPUT					= 30;		// gpio  input ģʽ
	const unsigned char		FC_GPIO_OUTPUT					= 31;		// gpio output ģʽ 
	const unsigned char		FC_GPIO_4_20MA					= 32;		// gpio 4~20ma ģʽ  
	
	//Modbus acsii  ynenЭ��  ��ʪ��
	const unsigned char     FC_MODBUS_ASCII_DEHUMIDITY		 	= 40;       //Modbus acsii  ��ʪЭ��
	
	//���ع���ģ��
	const unsigned char     FC_BATTERY_GET_V	 			= 0x60;       //���ԡ���ȡ���洢��ѹ
	const unsigned char     FC_BATTERY_GET_W	 			= 0x61;       //���ԡ���ȡ���洢�¶�
	const unsigned char     FC_BATTERY_GET_R	 			= 0x62;       //���ԡ���ȡ���洢����
	const unsigned char     FC_BATTERY_GET_I	 			= 0x63;       //���ԡ���ȡ���洢����

	//CAC �������������
	const unsigned char CAC_YX_VAL   = 1;
	const unsigned char CAC_YC_SHORT_L2H = 2;
	const unsigned char CAC_YC_SHORT_H2L = 3;
	const unsigned char CAC_YC_INT_L2H = 4;
	const unsigned char CAC_YC_INT_H2L = 5;
	const unsigned char CAC_YC_FLOAT_L2H = 6;
	const unsigned char CAC_YC_FLOAT_H2L = 7;
	const unsigned char CAC_YC_DOUBLE_L2H = 8;
	const unsigned char CAC_YC_DOUBLE_H2L = 9;
	const unsigned char CAC_DIC_YX_VAL   = 10;

};

class TForeProtocolBase
{
public:
	TForeProtocolBase();
	~TForeProtocolBase();

public:
	bool	Create( struct FORE_COMM_PARAM & CommParam, std::vector<string> & aryTermType );

	bool	IsPolling();
	bool	IsLinkModified();
	bool	IsLinkTimeout();

	bool	Loop();

	
	int 	Read( int len = 256, int select_wait_ms = 10 );
	int		Read_plc( unsigned char* recv_data, int read_len =256 );
	int		Read_byte( unsigned char* recv_data, int read_len , unsigned char  start_tag,  int  timeoutSec = 3000);

	int		Write();

	int		Send( XCollector &buf );

	bool    put_yx( int unit_addr, int order ,  char yxval );
	bool    put_yc( int unit_addr, int order ,  float ycval ); 
	 
public:
	 struct FORE_COMM_PARAM			m_CommParam;
	 std::vector<string>			m_aryTermType;

public:
	 

	XCollector			m_SendData;
	XCollector			m_RecvData;
 
	bool				m_bTcpNoblockMode;		//������ģʽ

	int			m_link_start_work_time;	//��ʼ����ʱ��
	 
	bool					m_bCloseTraceDataThread ;
	
#ifndef WIN32
	CSerial				m_serial;
#endif

protected:
	void Clear();
	void Destroy();

	bool InitComm();

	string	get_link_tip();
	string	get_protocol_name( int protocol );
	void	check_link_run_state();
	 
	void	create_trans_data_thread();
	void	terminate_trans_data_thread();
private:
	int				m_prev_link_run_state;	//ǰһ����·����״̬

	
	time_t			m_last_send_data_time;	//���һ�η�������ʱ��
	time_t			m_last_recv_data_time;	//���һ�ν�������ʱ��

	time_t			m_last_wirtelog_time;	//���һ��дlogʱ��
	 
	
#ifdef WIN32        
	HANDLE			m_trans_data_thread_handle;


	HANDLE			m_hCom;
	SimpleTcpClient   m_pTcpClient;
#else
	int				m_hCom;
	ZTcpClientSocket   m_pTcpClient;

	pthread_t			m_trans_data_thread_handle;
#endif

	
};
